import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router'
import store from '@/conf/store/index'

import {loadProcessBar, hideProcessBar, NProgressStart, NProgressEnd} from "@/assets/js/loading"
import {ElLoading} from "element-plus";

let routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        redirect: '/system/home'
    },
    {
        path: '/login',
        name: 'LOGIN',
        component: () => import('@/views/login/index.vue'),
        meta: {
            title: '登入',
            ignoreLogin: true
        }
    },
    {
        path: '/test',
        name: 'test',
        component: () => import('@/views/test1.vue'),
        meta: {
            title: '登入',
            ignoreLogin: true
        }
    },
    /**管理应用**/
    {
        path: '/system',
        name: 'System',
        component: () => import('@/views/home.vue'),
        children: [{
            name: 'webAdmin',
            path: '/system/:path*',
            component: () => import('@/views/error/404.vue'),
        }, {
            name: 'sysHome',
            path: '/system/home',
            component: () => import('@/views/user-center/home/index.vue'),
        }],
        meta: {
            title: '用户中心'
        }
    },
    {
        path: '/pageScreen',
        name: 'PageScreen',
        component: () => import('@/components/global/bigscreen/BigScreen.vue'),
        children: [{
            name: 'PageScreen404',
            path: '/:path*',
            component: () => import('@/views/error/404.vue'),
        }, {
            path: '/bigscreen',
            name: 'bigscreen',
            meta: {
                loadingBar: true //显示loadingbar 下载
            },
            component: () => import('@/views/bigscreen/index.vue'),
        },
            /**大屏实例2*/
            {
                path: '/bigscreen2',
                name: 'bigscreen2',
                meta: {
                    loadingBar: true //显示loadingbar 下载
                },
                component: () => import('@/views/bigscreen/bigscreen.vue'),
            }
        ]
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
})
let loadingInstance: any = null;

router.beforeEach((to, from, next) => {
    NProgressStart();
    loadingInstance = ElLoading.service({fullscreen: true});
    console.log(to.meta,"meta")
    if (to.matched.length > 0) {
        if (to.meta.loadingBar && to.meta.loadingBar == true) {
            loadProcessBar(2);
        }
        if ((to.meta['ignoreLogin'] === true) || store.state.Authorization) {
            next();
        } else {
            next({name: 'LOGIN'});
        }
    } else {
        next('404')
    }
});
router.afterEach((to, from, nex) => {
    NProgressEnd();
    if (loadingInstance != null) {
        loadingInstance.close();
    }
    setTimeout(function () {
        hideProcessBar();
    }, 1000)
})
export default router
